package o;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.gifdecoder.GifDecoder;
import com.bumptech.glide.gifdecoder.GifDecoder.c;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class ru implements GifDecoder {
    private static final String ˊ = ru.class.getSimpleName();
    private byte[] ʻ;
    private int ʻॱ;
    private byte[] ʼ;
    private byte[] ʽ;
    @NonNull
    private Config ʽॱ;
    @ColorInt
    private int[] ˊॱ;
    @ColorInt
    private int[] ˋ;
    private int ˋॱ;
    @ColorInt
    private final int[] ˎ;
    private ByteBuffer ˏ;
    private boolean ˏॱ;
    private rr ͺ;
    private final c ॱ;
    private Bitmap ॱˊ;
    private int ॱˋ;
    @Nullable
    private Boolean ॱˎ;
    private short[] ॱॱ;
    private int ॱᐝ;
    private byte[] ᐝ;
    private int ᐝॱ;

    public ru(@NonNull c cVar, rr rrVar, ByteBuffer byteBuffer, int i) {
        this(cVar);
        ˏ(rrVar, byteBuffer, i);
    }

    public ru(@NonNull c cVar) {
        this.ˎ = new int[256];
        this.ʽॱ = Config.ARGB_8888;
        this.ॱ = cVar;
        this.ͺ = new rr();
    }

    @NonNull
    public ByteBuffer ॱ() {
        return this.ˏ;
    }

    public void ˋ() {
        this.ˋॱ = (this.ˋॱ + 1) % this.ͺ.ˋ;
    }

    public int ˋ(int i) {
        if (i < 0 || i >= this.ͺ.ˋ) {
            return -1;
        }
        return ((rv) this.ͺ.ˎ.get(i)).ᐝ;
    }

    public int ˎ() {
        if (this.ͺ.ˋ <= 0 || this.ˋॱ < 0) {
            return 0;
        }
        return ˋ(this.ˋॱ);
    }

    public int ˊ() {
        return this.ͺ.ˋ;
    }

    public int ˏ() {
        return this.ˋॱ;
    }

    public void ᐝ() {
        this.ˋॱ = -1;
    }

    public int ʽ() {
        return (this.ˏ.limit() + this.ʻ.length) + (this.ˊॱ.length * 4);
    }

    @Nullable
    public synchronized Bitmap ʼ() {
        Bitmap bitmap;
        if (this.ͺ.ˋ <= 0 || this.ˋॱ < 0) {
            if (Log.isLoggable(ˊ, 3)) {
                Log.d(ˊ, "Unable to decode frame, frameCount=" + this.ͺ.ˋ + ", framePointer=" + this.ˋॱ);
            }
            this.ॱᐝ = 1;
        }
        if (this.ॱᐝ == 1 || this.ॱᐝ == 2) {
            if (Log.isLoggable(ˊ, 3)) {
                Log.d(ˊ, "Unable to decode frame, status=" + this.ॱᐝ);
            }
            bitmap = null;
        } else {
            rv rvVar;
            int[] iArr;
            this.ॱᐝ = 0;
            if (this.ʼ == null) {
                this.ʼ = this.ॱ.ˋ(255);
            }
            rv rvVar2 = (rv) this.ͺ.ˎ.get(this.ˋॱ);
            int i = this.ˋॱ - 1;
            if (i >= 0) {
                rvVar = (rv) this.ͺ.ˎ.get(i);
            } else {
                rvVar = null;
            }
            if (rvVar2.ˋॱ != null) {
                iArr = rvVar2.ˋॱ;
            } else {
                iArr = this.ͺ.ॱ;
            }
            this.ˋ = iArr;
            if (this.ˋ == null) {
                if (Log.isLoggable(ˊ, 3)) {
                    Log.d(ˊ, "No valid color table found for frame #" + this.ˋॱ);
                }
                this.ॱᐝ = 1;
                bitmap = null;
            } else {
                if (rvVar2.ʼ) {
                    System.arraycopy(this.ˋ, 0, this.ˎ, 0, this.ˋ.length);
                    this.ˋ = this.ˎ;
                    this.ˋ[rvVar2.ॱॱ] = 0;
                    if (rvVar2.ʻ == 2 && this.ˋॱ == 0) {
                        this.ॱˎ = Boolean.valueOf(true);
                    }
                }
                bitmap = ˋ(rvVar2, rvVar);
            }
        }
        return bitmap;
    }

    public void ʻ() {
        this.ͺ = null;
        if (this.ʻ != null) {
            this.ॱ.ˊ(this.ʻ);
        }
        if (this.ˊॱ != null) {
            this.ॱ.ˏ(this.ˊॱ);
        }
        if (this.ॱˊ != null) {
            this.ॱ.ˎ(this.ॱˊ);
        }
        this.ॱˊ = null;
        this.ˏ = null;
        this.ॱˎ = null;
        if (this.ʼ != null) {
            this.ॱ.ˊ(this.ʼ);
        }
    }

    public synchronized void ˏ(@NonNull rr rrVar, @NonNull ByteBuffer byteBuffer, int i) {
        if (i <= 0) {
            throw new IllegalArgumentException("Sample size must be >=0, not: " + i);
        }
        int highestOneBit = Integer.highestOneBit(i);
        this.ॱᐝ = 0;
        this.ͺ = rrVar;
        this.ˋॱ = -1;
        this.ˏ = byteBuffer.asReadOnlyBuffer();
        this.ˏ.position(0);
        this.ˏ.order(ByteOrder.LITTLE_ENDIAN);
        this.ˏॱ = false;
        for (rv rvVar : rrVar.ˎ) {
            if (rvVar.ʻ == 3) {
                this.ˏॱ = true;
                break;
            }
        }
        this.ᐝॱ = highestOneBit;
        this.ॱˋ = rrVar.ᐝ / highestOneBit;
        this.ʻॱ = rrVar.ॱॱ / highestOneBit;
        this.ʻ = this.ॱ.ˋ(rrVar.ᐝ * rrVar.ॱॱ);
        this.ˊॱ = this.ॱ.ˏ(this.ॱˋ * this.ʻॱ);
    }

    public void ॱ(@NonNull Config config) {
        if (config == Config.ARGB_8888 || config == Config.RGB_565) {
            this.ʽॱ = config;
            return;
        }
        throw new IllegalArgumentException("Unsupported format: " + config + ", must be one of " + Config.ARGB_8888 + " or " + Config.RGB_565);
    }

    private Bitmap ˋ(rv rvVar, rv rvVar2) {
        int[] iArr = this.ˊॱ;
        if (rvVar2 == null) {
            if (this.ॱˊ != null) {
                this.ॱ.ˎ(this.ॱˊ);
            }
            this.ॱˊ = null;
            Arrays.fill(iArr, 0);
        }
        if (rvVar2 != null && rvVar2.ʻ == 3 && this.ॱˊ == null) {
            Arrays.fill(iArr, 0);
        }
        if (rvVar2 != null && rvVar2.ʻ > 0) {
            if (rvVar2.ʻ == 2) {
                int i;
                if (rvVar.ʼ) {
                    i = 0;
                } else {
                    i = this.ͺ.ˋॱ;
                    if (rvVar.ˋॱ != null && this.ͺ.ʽ == rvVar.ॱॱ) {
                        i = 0;
                    }
                }
                int i2 = rvVar2.ˏ / this.ᐝॱ;
                int i3 = rvVar2.ॱ / this.ᐝॱ;
                int i4 = ((rvVar2.ˎ / this.ᐝॱ) * this.ॱˋ) + i3;
                i3 = i4 + ((rvVar2.ˋ / this.ᐝॱ) * this.ॱˋ);
                while (i4 < i3) {
                    int i5 = i4 + i2;
                    for (int i6 = i4; i6 < i5; i6++) {
                        iArr[i6] = i;
                    }
                    i4 += this.ॱˋ;
                }
            } else if (rvVar2.ʻ == 3 && this.ॱˊ != null) {
                this.ॱˊ.getPixels(iArr, 0, this.ॱˋ, 0, 0, this.ॱˋ, this.ʻॱ);
            }
        }
        ˏ(rvVar);
        if (rvVar.ˊ || this.ᐝॱ != 1) {
            ॱ(rvVar);
        } else {
            ˊ(rvVar);
        }
        if (this.ˏॱ && (rvVar.ʻ == 0 || rvVar.ʻ == 1)) {
            if (this.ॱˊ == null) {
                this.ॱˊ = ˋॱ();
            }
            this.ॱˊ.setPixels(iArr, 0, this.ॱˋ, 0, 0, this.ॱˋ, this.ʻॱ);
        }
        Bitmap ˋॱ = ˋॱ();
        ˋॱ.setPixels(iArr, 0, this.ॱˋ, 0, 0, this.ॱˋ, this.ʻॱ);
        return ˋॱ;
    }

    private void ˊ(rv rvVar) {
        int[] iArr = this.ˊॱ;
        int i = rvVar.ˋ;
        int i2 = rvVar.ˎ;
        int i3 = rvVar.ˏ;
        int i4 = rvVar.ॱ;
        Object obj = this.ˋॱ == 0 ? 1 : null;
        int i5 = this.ॱˋ;
        byte[] bArr = this.ʻ;
        int[] iArr2 = this.ˋ;
        int i6 = -1;
        int i7 = 0;
        while (i7 < i) {
            int i8 = (i7 + i2) * i5;
            int i9 = i8 + i4;
            int i10 = i9 + i3;
            if (i8 + i5 < i10) {
                i10 = i8 + i5;
            }
            int i11 = i9;
            i8 = i6;
            i6 = rvVar.ˏ * i7;
            while (i11 < i10) {
                byte b = bArr[i6];
                int i12 = b & 255;
                if (i12 != i8) {
                    i12 = iArr2[i12];
                    if (i12 != 0) {
                        iArr[i11] = i12;
                    } else {
                        byte b2 = b;
                    }
                }
                i11++;
                i6++;
            }
            i7++;
            i6 = i8;
        }
        boolean z = (this.ॱˎ != null && this.ॱˎ.booleanValue()) || !(this.ॱˎ != null || obj == null || i6 == -1);
        this.ॱˎ = Boolean.valueOf(z);
    }

    private void ॱ(rv rvVar) {
        int[] iArr = this.ˊॱ;
        int i = rvVar.ˋ / this.ᐝॱ;
        int i2 = rvVar.ˎ / this.ᐝॱ;
        int i3 = rvVar.ˏ / this.ᐝॱ;
        int i4 = rvVar.ॱ / this.ᐝॱ;
        int i5 = 1;
        int i6 = 8;
        int i7 = 0;
        Object obj = this.ˋॱ == 0 ? 1 : null;
        int i8 = this.ᐝॱ;
        int i9 = this.ॱˋ;
        int i10 = this.ʻॱ;
        byte[] bArr = this.ʻ;
        int[] iArr2 = this.ˋ;
        Boolean bool = this.ॱˎ;
        int i11 = 0;
        while (i11 < i) {
            int i12;
            int i13;
            int i14;
            Object obj2;
            Boolean bool2;
            if (rvVar.ˊ) {
                if (i7 >= i) {
                    i5++;
                    switch (i5) {
                        case 2:
                            i7 = 4;
                            break;
                        case 3:
                            i7 = 2;
                            i6 = 4;
                            break;
                        case 4:
                            i7 = 1;
                            i6 = 2;
                            break;
                    }
                }
                i12 = i7 + i6;
                i13 = i6;
                i14 = i5;
            } else {
                i12 = i7;
                i13 = i6;
                i14 = i5;
                i7 = i11;
            }
            i6 = i7 + i2;
            if (i8 == 1) {
                obj2 = 1;
            } else {
                obj2 = null;
            }
            if (i6 < i10) {
                i6 *= i9;
                i5 = i6 + i4;
                i7 = i5 + i3;
                if (i6 + i9 < i7) {
                    i7 = i6 + i9;
                }
                i6 = (i11 * i8) * rvVar.ˏ;
                int i15;
                int i16;
                int i17;
                if (obj2 != null) {
                    i15 = i6;
                    bool2 = bool;
                    i5 = i15;
                    for (i16 = i5; i16 < i7; i16++) {
                        i17 = iArr2[bArr[i5] & 255];
                        if (i17 != 0) {
                            iArr[i16] = i17;
                        } else if (obj != null && r4 == null) {
                            bool2 = Boolean.valueOf(true);
                        }
                        i5 += i8;
                    }
                } else {
                    i17 = ((i7 - i5) * i8) + i6;
                    i15 = i6;
                    bool2 = bool;
                    i5 = i15;
                    for (i16 = i5; i16 < i7; i16++) {
                        int ˎ = ˎ(i5, i17, rvVar.ˏ);
                        if (ˎ != 0) {
                            iArr[i16] = ˎ;
                        } else if (obj != null && r4 == null) {
                            bool2 = Boolean.valueOf(true);
                        }
                        i5 += i8;
                    }
                }
            } else {
                bool2 = bool;
            }
            i11++;
            bool = bool2;
            i7 = i12;
            i5 = i14;
            i6 = i13;
        }
        if (this.ॱˎ == null) {
            boolean z;
            if (bool == null) {
                z = false;
            } else {
                z = bool.booleanValue();
            }
            this.ॱˎ = Boolean.valueOf(z);
        }
    }

    @ColorInt
    private int ˎ(int i, int i2, int i3) {
        int i4 = i;
        int i5 = 0;
        int i6 = 0;
        int i7 = 0;
        int i8 = 0;
        int i9 = 0;
        while (i4 < this.ᐝॱ + i && i4 < this.ʻ.length && i4 < i2) {
            int i10 = this.ˋ[this.ʻ[i4] & 255];
            if (i10 != 0) {
                i9 += (i10 >> 24) & 255;
                i8 += (i10 >> 16) & 255;
                i7 += (i10 >> 8) & 255;
                i6 += i10 & 255;
                i5++;
            }
            i4++;
        }
        i4 = i + i3;
        while (i4 < (i + i3) + this.ᐝॱ && i4 < this.ʻ.length && i4 < i2) {
            i10 = this.ˋ[this.ʻ[i4] & 255];
            if (i10 != 0) {
                i9 += (i10 >> 24) & 255;
                i8 += (i10 >> 16) & 255;
                i7 += (i10 >> 8) & 255;
                i6 += i10 & 255;
                i5++;
            }
            i4++;
        }
        if (i5 == 0) {
            return 0;
        }
        return ((((i9 / i5) << 24) | ((i8 / i5) << 16)) | ((i7 / i5) << 8)) | (i6 / i5);
    }

    private void ˏ(rv rvVar) {
        int i;
        int i2;
        if (rvVar != null) {
            this.ˏ.position(rvVar.ʽ);
        }
        if (rvVar == null) {
            i = this.ͺ.ᐝ * this.ͺ.ॱॱ;
        } else {
            i = rvVar.ˏ * rvVar.ˋ;
        }
        if (this.ʻ == null || this.ʻ.length < i) {
            this.ʻ = this.ॱ.ˋ(i);
        }
        byte[] bArr = this.ʻ;
        if (this.ॱॱ == null) {
            this.ॱॱ = new short[4096];
        }
        short[] sArr = this.ॱॱ;
        if (this.ʽ == null) {
            this.ʽ = new byte[4096];
        }
        byte[] bArr2 = this.ʽ;
        if (this.ᐝ == null) {
            this.ᐝ = new byte[4097];
        }
        byte[] bArr3 = this.ᐝ;
        int ॱॱ = ॱॱ();
        int i3 = 1 << ॱॱ;
        int i4 = i3 + 1;
        int i5 = i3 + 2;
        int i6 = ॱॱ + 1;
        int i7 = (1 << i6) - 1;
        for (i2 = 0; i2 < i3; i2++) {
            sArr[i2] = (short) 0;
            bArr2[i2] = (byte) i2;
        }
        byte[] bArr4 = this.ʼ;
        int i8 = 0;
        i2 = 0;
        int i9 = 0;
        int i10 = 0;
        int i11 = -1;
        int i12 = i6;
        int i13 = i7;
        int i14 = i5;
        i6 = 0;
        i5 = 0;
        i7 = 0;
        int i15 = 0;
        while (i8 < i) {
            if (i7 == 0) {
                i7 = ͺ();
                if (i7 <= 0) {
                    this.ॱᐝ = 3;
                    break;
                }
                i6 = 0;
            }
            i5 += (bArr4[i6] & 255) << i15;
            int i16 = i6 + 1;
            int i17 = i7 - 1;
            i6 = i12;
            i7 = i13;
            i13 = i8;
            i8 = i2;
            i2 = i10;
            int i18 = i11;
            i11 = i5;
            i5 = i14;
            i14 = i15 + 8;
            i15 = i18;
            while (i14 >= i6) {
                i10 = i11 & i7;
                i12 = i11 >> i6;
                i14 -= i6;
                if (i10 == i3) {
                    i6 = ॱॱ + 1;
                    i7 = (1 << i6) - 1;
                    i5 = i3 + 2;
                    i11 = i12;
                    i15 = -1;
                } else if (i10 == i4) {
                    i10 = i2;
                    i11 = i15;
                    i2 = i8;
                    i15 = i14;
                    i8 = i13;
                    i14 = i5;
                    i5 = i12;
                    i13 = i7;
                    i12 = i6;
                    i7 = i17;
                    i6 = i16;
                    break;
                } else if (i15 == -1) {
                    bArr[i8] = bArr2[i10];
                    i11 = i12;
                    i13++;
                    i15 = i10;
                    i8++;
                    i2 = i10;
                } else {
                    if (i10 >= i5) {
                        bArr3[i9] = (byte) i2;
                        i9++;
                        i2 = i15;
                    } else {
                        i2 = i10;
                    }
                    while (i2 >= i3) {
                        bArr3[i9] = bArr2[i2];
                        i9++;
                        i2 = sArr[i2];
                    }
                    i11 = bArr2[i2] & 255;
                    bArr[i8] = (byte) i11;
                    i2 = i8 + 1;
                    i8 = i13 + 1;
                    while (i9 > 0) {
                        i9--;
                        bArr[i2] = bArr3[i9];
                        i2++;
                        i8++;
                    }
                    if (i5 < 4096) {
                        sArr[i5] = (short) i15;
                        bArr2[i5] = (byte) i11;
                        i5++;
                        if ((i5 & i7) == 0 && i5 < 4096) {
                            i6++;
                            i7 += i5;
                        }
                    }
                    i13 = i8;
                    i15 = i10;
                    i8 = i2;
                    i2 = i11;
                    i11 = i12;
                }
            }
            i10 = i2;
            i12 = i6;
            i2 = i8;
            i6 = i16;
            i8 = i13;
            i13 = i7;
            i7 = i17;
            i18 = i15;
            i15 = i14;
            i14 = i5;
            i5 = i11;
            i11 = i18;
        }
        Arrays.fill(bArr, i2, i, (byte) 0);
    }

    private int ॱॱ() {
        return this.ˏ.get() & 255;
    }

    private int ͺ() {
        int ॱॱ = ॱॱ();
        if (ॱॱ > 0) {
            this.ˏ.get(this.ʼ, 0, Math.min(ॱॱ, this.ˏ.remaining()));
        }
        return ॱॱ;
    }

    private Bitmap ˋॱ() {
        Config config = (this.ॱˎ == null || this.ॱˎ.booleanValue()) ? Config.ARGB_8888 : this.ʽॱ;
        Bitmap ˋ = this.ॱ.ˋ(this.ॱˋ, this.ʻॱ, config);
        ˋ.setHasAlpha(true);
        return ˋ;
    }
}
